import { makeAutoObservable } from 'mobx'
import { GetDictionary } from '../requests'
import auth from './auth'

export default new (class Auto {
    categories: string[][] = []
    category: string[] = []
    models: string[][] = []
    model: string[] = []
    cities: string[][] = []
    city: string[] = []
    constructor() {
        makeAutoObservable(this)
    }
    SetCities() {
        GetDictionary('CITIES').then((data) => {
            this.cities = []
            if (data.statusCode == '401') {
                auth.LogOut()
            } else {
                for (const i in data) {
                    this.cities.push([data[i].name, data[i].code])
                }
            }
        })
    }
    SetCategories() {
        GetDictionary('AUTO_CATEGORIES').then((data) => {
            this.categories = []
            if (data.statusCode == '401') {
                auth.LogOut()
            } else {
                for (const i in data) {
                    this.categories.push([data[i].name, data[i].code, data[i].id])
                }
            }
        })
    }
    SetModels(category: string) {
        this.models = []
        GetDictionary('AUTO').then((data) => {
            if (data.statusCode == '401') {
                auth.LogOut()
            } else {
                for (const i in data) {
                    if (data[i].categoryId == category) this.models.push([data[i].name, data[i].code])
                }
            }
        })
    }
})()
